package com.wenx.v3system.modular.cloud.domain.query;

import com.wenx.base.domain.BaseQuery;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import jakarta.validation.constraints.Min;

/**
 * 统一部门查询DTO
 * 整合了部门成员查询、部门信息查询等功能
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Schema(description = "部门查询参数")
public class DepartmentQuery extends BaseQuery {

    // ========== 部门基本信息查询字段 ==========
    
    @Schema(description = "部门ID", example = "1001")
    private Long departmentId;

    @Schema(description = "部门名称", example = "技术部")
    private String departmentName;

    @Schema(description = "部门编码", example = "TECH")
    private String departmentCode;

    @Schema(description = "父部门ID", example = "1000")
    private Long parentId;

    @Schema(description = "部门状态：1-启用，0-禁用", example = "1")
    private Integer departmentStatus;

    // ========== 部门成员查询字段 ==========
    
    @Schema(description = "关键词（用户名、姓名、职位）", example = "张三")
    private String keyword;

    @Schema(description = "职位", example = "高级工程师")
    private String position;

    @Schema(description = "用户状态：1-启用，0-禁用", example = "1")
    private Integer userStatus;

    @Schema(description = "用户ID", example = "2001")
    private Long userId;

    // ========== 查询类型标识 ==========
    
    @Schema(description = "查询类型：DEPARTMENT-部门信息，MEMBER-部门成员，TREE-部门树", 
            example = "MEMBER", allowableValues = {"DEPARTMENT", "MEMBER", "TREE"})
    private String queryType;

    // ========== 分页参数 ==========
    
    @Min(value = 1, message = "当前页码必须大于0")
    @Schema(description = "当前页码", example = "1", defaultValue = "1")
    private Long page = 1L;

    @Min(value = 1, message = "每页大小必须大于0")
    @Schema(description = "每页大小", example = "10", defaultValue = "10")
    private Long size = 10L;

    @Override
    public long getSize() {
        return size != null ? size : 10L;
    }

    @Override
    public long getCurrent() {
        return page != null ? page : 1L;
    }

    /**
     * 创建部门成员查询
     */
    public static DepartmentQuery forMembers(Long departmentId) {
        DepartmentQuery query = new DepartmentQuery();
        query.setDepartmentId(departmentId);
        query.setQueryType("MEMBER");
        return query;
    }

    /**
     * 创建部门成员查询（带关键词）
     */
    public static DepartmentQuery forMembers(Long departmentId, String keyword) {
        DepartmentQuery query = forMembers(departmentId);
        query.setKeyword(keyword);
        return query;
    }

    /**
     * 创建部门信息查询
     */
    public static DepartmentQuery forDepartment(Long departmentId) {
        DepartmentQuery query = new DepartmentQuery();
        query.setDepartmentId(departmentId);
        query.setQueryType("DEPARTMENT");
        return query;
    }

    /**
     * 创建部门树查询
     */
    public static DepartmentQuery forTree() {
        DepartmentQuery query = new DepartmentQuery();
        query.setQueryType("TREE");
        return query;
    }

    /**
     * 创建部门树查询（指定父部门）
     */
    public static DepartmentQuery forTree(Long parentId) {
        DepartmentQuery query = forTree();
        query.setParentId(parentId);
        return query;
    }

    // ========== 验证方法 ==========

    /**
     * 验证是否为有效的部门成员查询
     */
    public boolean isValidForMemberQuery() {
        return "MEMBER".equals(queryType) && departmentId != null;
    }

    /**
     * 验证是否为有效的部门信息查询
     */
    public boolean isValidForDepartmentQuery() {
        return "DEPARTMENT".equals(queryType);
    }

    /**
     * 验证是否为有效的部门树查询
     */
    public boolean isValidForTreeQuery() {
        return "TREE".equals(queryType);
    }

    // ========== 便捷方法 ==========

    /**
     * 设置分页参数
     */
    public DepartmentQuery withPagination(Long page, Long size) {
        this.page = page;
        this.size = size;
        return this;
    }

    /**
     * 设置关键词搜索
     */
    public DepartmentQuery withKeyword(String keyword) {
        this.keyword = keyword;
        return this;
    }

    /**
     * 设置职位筛选
     */
    public DepartmentQuery withPosition(String position) {
        this.position = position;
        return this;
    }

    /**
     * 设置用户状态筛选
     */
    public DepartmentQuery withUserStatus(Integer userStatus) {
        this.userStatus = userStatus;
        return this;
    }

    /**
     * 设置部门状态筛选
     */
    public DepartmentQuery withDepartmentStatus(Integer departmentStatus) {
        this.departmentStatus = departmentStatus;
        return this;
    }
}